import ajax from "../../utils/ajax";
import util from "../../utils/util";
import answer_work from "../../utils/answer_work";
import areaList from "../../utils/area";
// import { startGlobalTimer } from '../../utils/timer'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    idcard: '',
    array: [],
    index: 0,
    unit: '',
    areaArray: [],
    areaIndex: 0,
    area: '',
    areaId:'',
    unitId:''
  },
  onShow(){
    // 截屏
    wx.onUserCaptureScreen(() => {  
      // 立即弹出警告  
      wx.showModal({  
        title: "禁止截屏",  
        content: "同学，您的截屏动作已被后台记录，再次截屏将会自动取消成绩！",  
        showCancel: false  
      }); 
      const userInfo = wx.getStorageSync('userInfo');
      const paperId = wx.getStorageSync('paperId');
      const testType = wx.getStorageSync('testType');
      console.log(testType)
      let idCard = '';
      let userName = '';
      
      if (userInfo) {
        idCard = userInfo.idcard;
        userName = userInfo.name;
      }
      
      // 提交截图者的身份信息
      ajax.postMethod('small/app/add/screenCapture', {
        userName:userName,
        answerWork: userInfo.unitId,
        idCard: idCard,
        testId:paperId,
        testType:testType 
      }).then(res => {
        console.log('截图信息提交成功', res)
      }).catch(err => {
        console.error('截图信息提交失败', err);
      });
    });
  },
  onLoad() {
    util.checkJump();
    this.getUnitData();
    this.getAreaData();
  },
  // getUnitData() {
  //   ajax.getMethod('small/app/getDict/answer_work').then(res => {
  //     const newData = res.data.map(item => item.dictLabel);
  //     this.setData({
  //       array: res.data,
  //       arrayList:newData
  //     });
  //   });
  // },
  // getAreaData() {
  //   ajax.getMethod('system/area/list/pid?parentId=0').then(res => {
  //     const newData = res.map(item => item.areaName);
  //     this.setData({
  //       areaArray: res,
  //       areaArrayList: newData
  //     });
  //   });
  // },
  getUnitData() {
    const res = answer_work;
    const newData = res.map(item => item.dictLabel);
    this.setData({
      array: res,
      arrayList:newData
    });
  },
  getAreaData() {
    const res = areaList;
    const newData = res.map(item => item.areaName);
    this.setData({
      areaArray: res,
      areaArrayList: newData
    });      
  },
  onInputName(e) {
    this.setData({
      name: e.detail.value
    });
  },
  onInputIdCard(e) {
    this.setData({
      idcard: e.detail.value
    });
  },
  // 选择单位
  bindPickerChange: function (e) {
    let array = this.data.array;
    if(!array){
      this.getUnitData();
    }
    this.setData({
      unit: array[e.detail.value].dictLabel,
      unitId: array[e.detail.value].dictValue
    })
    wx.setStorageSync('unitId', this.data.unitId);
  },
  // 选择省份
  onChange: function (e) {
    let areaArray = this.data.areaArray;
    if(!areaArray){
      this.getAreaData();
    }
    this.setData({
      area: areaArray[e.detail.value].areaName,
      areaId: areaArray[e.detail.value].areaId
    })
  },
  /**
   * 身份证号码校验
   * @param {string} idCard - 身份证号码
   * @returns {boolean} 校验结果
   */
  validateIdCard(idCard) {
    // 18位身份证校验
    const reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    return reg.test(idCard);
  },
  // 登录
  async onLogin() {
    let {
      name,
      idcard,
      unit,
      area,
      unitId,
      areaId
    } = this.data;
    if(!name){
      wx.showToast({
        title: '请输入姓名',
        icon:'none'
      })
      return;
    }
    if(!idcard){
      wx.showToast({
        title: '请输入身份证号',
        icon:'none'
      })
      return;
    }
    // 身份证格式校验
    if (!this.validateIdCard(idcard)) {
      wx.showToast({
        title: '身份证格式不正确',
        icon: 'none'
      })
      return;
    }
    if(!unit){
      wx.showToast({
        title: '请选择报考单位',
        icon:'none'
      })
      return;
    }
    if(!area){
      wx.showToast({
        title: '请选择省份',
        icon:'none'
      })
      return;
    }
    // 将数据存入缓存
    wx.setStorageSync('userInfo', {
      name,
      idcard,
      unitId,
      areaId
    });
    // startGlobalTimer();//开始计时
    // wx.setStorageSync('startTime', Date.now());//记录开始答题的时间戳
    // 跳转介绍页面

    const res = await ajax.postMethod('small/app/wx/login',{
      idCard: idcard,
      name,
      answerWork:unitId
    },'登录中...')

    if(res.code=='200'){
      this.getTopicData();
    }else{
      wx.showToast({
        title: res.msg,
        icon:'none'
      })
    }
  },
  // 获取认知题目
  async getTopicData() {
    const userInfo = wx.getStorageSync('userInfo');
    let idcard = userInfo.idcard;
    let getType = await ajax.getMethod('small/app/paper/list/'+ idcard+'/'+userInfo.unitId);
    if(getType.code !== 200){
      wx.showToast({
        title: getType.msg,
        icon:'none'
      })
    }
    let type = getType.data.find(item => item.ptype == 1);
 
    let anotherType = getType.data.find(item => item.ptype === 2);
    const mentalInfo = getType.data.find(item => item.ptype === 3);
    //胜任
    wx.setStorageSync('anotherType',anotherType?.paperId);
    //心理
    wx.setStorageSync('mentalId',mentalInfo?.paperId);

    //认知
    wx.setStorageSync('bussId',type?.paperId);
    //当前试卷id
    wx.setStorageSync('paperId',type?.paperId);
    wx.setStorageSync('testType',2);
    const answerWork = userInfo.unitId
    const res = await ajax.getMethod('small/app/paper/info/' + type.paperId + '/' +idcard+'/'+answerWork)
    // console.log(res)
    const questionList = res.data.paperQuestionTypes;
    const questionNumber = 0;
    // 将 questionList 存入缓存
    wx.setStorageSync('questionInfo', {
      questionList,
      questionNumber
    });
   
    // 跳转对应页面
    if(type?.completed!=1){
      wx.reLaunch({
        url: "../introduce/introduce"
      })
     
    }
    // 答题数据缓存
    wx.setStorageSync('answerBaseArray', {
      name:userInfo.name,
      cardId:userInfo.idcard,
      instructorName:'',
      instructorId:0,
      paperId:type?.paperId,
      deptId:'',
      areaId:userInfo.areaId,
      answerWork:userInfo.unitId
    });

    if(type?.completed==1){
      //全部完成考试
      if(anotherType?.completed==1&&mentalInfo?.completed==1){
        wx.showToast({
          title: '你已完成考试',
          icon:'none'
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '../index/index?cleaned=1',
          })
        }, 1000);
       
      }else if(anotherType?.completed==1){
        wx.reLaunch({
          url: '../topic-another/topic-another?id='+ mentalInfo?.paperId
        })
      }else{
        wx.reLaunch({
          url: '../topic-another/topic-another?id='+ anotherType?.paperId
        })
      }
      return
    }
  }
})